//
//  HLNetTool.m
//  Magic-C
//
//  Created by 赵五一 on 16/7/13.
//  Copyright © 2016年 赵五一. All rights reserved.
//

#import "HLNetTool.h"
#import "AFNetworking.h"

@implementation HLNetTool

+ (NSURLSessionDataTask *)GET:(NSString *)url body:(id)body header:(NSDictionary *)headers response:(HLResponseStyle)responseStyle successBlock:(successBlock)success failureBlock:(failureBlock)failure
{
    // 1. 设置网络管理者
    AFHTTPSessionManager *manager = [HLNetTool setupManagerWithURL:url body:body headers:headers responseStyle:responseStyle];
    
    NSURLSessionDataTask *dataTask = [manager GET:url parameters:body progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (responseObject) {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (error) {
            failure(error);
        }
    }];
    return dataTask;
}


+(NSURLSessionDataTask *)POST:(NSString *)url body:(id)body header:(NSDictionary *)headers response:(HLResponseStyle)responseStyle successBlock:(successBlock)success failureBlock:(failureBlock)failure{
    // 1. 设置网络管理者
    AFHTTPSessionManager *manager = [HLNetTool setupManagerWithURL:url body:body headers:headers responseStyle:responseStyle];
    
    NSURLSessionDataTask *dataTask = [manager POST:url parameters:body progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (responseObject) {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (error) {
            failure(error);
        }
    }];
    return dataTask;
}

+ (AFHTTPSessionManager *)setupManagerWithURL:(NSString *)url body:(id)body headers:(NSDictionary *)headers responseStyle:(HLResponseStyle)responseStyle
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    // 1
    if ([body isKindOfClass:[NSString class]]) {
        [manager.requestSerializer setQueryStringSerializationWithBlock:^NSString * _Nonnull(NSURLRequest * _Nonnull request, id  _Nonnull parameters, NSError * _Nullable * _Nullable error) {
            return parameters;
        }];
    } else {
        manager.requestSerializer = [AFJSONRequestSerializer serializer];
    }
            
    // 2
    if (headers) {
        for (NSString *key in headers.allKeys) {
            [manager.requestSerializer setValue:headers[key] forHTTPHeaderField:key];
        }
    }
    
    // 3. 设置返回数据的类型
    switch (responseStyle) {
        case HLJSON:
            manager.responseSerializer = [AFJSONResponseSerializer serializer];
            break;
        case HLXML:
            manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
            break;
        case HLDATA:
            manager.responseSerializer = [AFHTTPResponseSerializer serializer];
            break;
        default:
            break;
    }
    
    // 4. 设置响应数据类型
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css",@"text/plain", @"application/javascript",@"image/jpeg", @"text/vnd.wap.wml", @"text/xml", @"application/x-javascript", nil]];
    
    // 5. UTF-8转码
    url = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];

    return manager;
}



@end
